using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.Skill
{
    public class SkillCasterBase:IClass
    {
        public virtual void OnReset()
        {
            bulletStateList.Clear();
            targets = null;
        }

        protected PveSkillConfig config;
        protected int childSkillIndex;
        protected List<bool> bulletStateList = new List<bool>();
        protected List<UnitBase> targets;
        protected float stateStartTime;
        protected SkillBase skill;
        protected UnitBase unit;

        public virtual bool IsCasting()
        {
            return false;
        }
        
        public void SetTargets(List<UnitBase> targets)
        {
            this.targets = targets;
        }
        
        public void Prepare()
        {
            bulletStateList.Clear();
            for (int j = 0; j < config.bullets.Count; j++)
            {
                bulletStateList.Add(false);
            }
        }

        public virtual void Start(float stateStartTime)
        {
            this.stateStartTime = stateStartTime;
        }
        
        public virtual void Init(SkillBase skill,PveSkillConfig config,int childSkillIndex)
        {
            this.skill = skill;
            this.config = config;
            this.childSkillIndex = childSkillIndex;
            this.unit = skill.unit;
        }

        public virtual void Dispose()
        {
            
        }

        public virtual void End()
        {
            
        }

        public virtual void Update(float currentTime)
        {
            for (int i = 0; i < config.bullets.Count; i++)
            {
                if (!bulletStateList[i] && config.bullets[i].z >= 0 && currentTime - stateStartTime >= config.bullets[i].x / 30f)
                {
                    //PlayBulletSound();
                    var offset = skill.GetBulletOffset((int)config.bullets[i].z);
                    var bulletConfig = PveUtils.GetSkillBulletConfig((int) config.bullets[i].y);
                    if (bulletConfig.director_type == 5)//按照之前随机的方向
                    {
                        skill.CreateBulletToTargetPosition(i,offset,(int)config.bullets[i].y,childSkillIndex,config.bullets[i].z);
                    }
                    else if (bulletConfig.director_type == 0 || bulletConfig.damage_type == 1)//向targetList方向
                    {
                        skill.CreateBulletByTargets(offset,(int)config.bullets[i].y,childSkillIndex,config.bullets[i].z,i);
                    }
                    else if (bulletConfig.director_type == 6)
                    {
                        skill.CreateBulletBySngleTarget(offset,(int)config.bullets[i].y,childSkillIndex,config.bullets[i].z,i);

                    }
                    else//向自身方向
                    {
                        skill.CreateBulletByCertainForward(offset,(int)config.bullets[i].y,childSkillIndex,config.bullets[i].z);
                    }
                    
                    bulletStateList[i] = true;
                }
            }
        }

        public virtual void ForceEnd()
        {
            End();
        }
    }
}